﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace MipsPipelineSimulator
{
    public partial class MainForm : Form
    {
        private CommandForm commandForm = null;

        private Image image = null;

        private Graphics [] gs = null;

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            
            g.DrawImage(image,new Point(0,0));
        }

        private void KeepStatusToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
        }

        private void QuitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void InputCommandToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (commandForm == null)
            {
                commandForm = new CommandForm(this);
                commandForm.Height = this.Height;
            }
            //commandForm.SetPosition();
            commandForm.WindowState = System.Windows.Forms.FormWindowState.Normal;
            commandForm.Show();
            commandForm.Activate();
        }

        private void PauseToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void QuitExecuteToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void AboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 5; i++)
            {
                Animate animate = new Animate() ;
                animate.direction = Direction.LeftToRight;
                animate.forward = 1;
                animate.spanTime = 10;
                animate.g = this.GetGraphics();
                animate.line.from = new Point(10, 50 + i * 20);
                animate.line.end = new Point(600, 50 + i * 20);
                animate.line.pen = new Pen(Color.Red, 2);
                animate.distance = 3;
                animate.brush = new SolidBrush(Color.Blue);
                animate.font = new Font("宋体", 12);
                animate.ifDisplayText = true;
                animate.text = "123";
                DrawPaths tmp = new DrawPaths();
                tmp.animate = new Animate[1] {animate};
                tmp.control = this;
                CommonTool.StartDrawPathsThread(tmp);
            }

            //for (int i = 0; i < 5; i++)
            //{
            //    Animate animate = new Animate();
            //    animate.direction = Direction.TopToBottom;
            //    animate.forward = 10;
            //    animate.spanTime = 10;
            //    animate.g = this.GetGraphics();
            //    animate.line.from = new Point(400 + i * 20, 10);
            //    animate.line.end = new Point(400 + i * 20, 400);
            //    animate.line.pen = new Pen(Color.Red, 3);
            //    DrawPaths tmp = new DrawPaths();
            //    tmp.animate = new Animate[1] { animate };
            //    tmp.control = this;
            //    CommonTool.StartDrawPathsThread(tmp);
            //}

            //for (int i = 0; i < 5; i++)
            //{
            //    Animate animate = new Animate();
            //    animate.direction = Direction.BottomToTop;
            //    animate.forward = 10;
            //    animate.spanTime = 10;
            //    animate.g = this.GetGraphics();
            //    animate.line.from = new Point(200 + i * 20, 400);
            //    animate.line.end = new Point(200 + i * 20, 10);
            //    animate.line.pen = new Pen(Color.Red, 3);
            //    DrawPaths tmp = new DrawPaths();
            //    tmp.animate = new Animate[1] { animate };
            //    tmp.control = this;
            //    CommonTool.StartDrawPathsThread(tmp);
            //}

            //for (int i = 0; i < 5; i++)
            //{
            //    Animate animate = new Animate();
            //    animate.direction = Direction.RightToLeft;
            //    animate.forward = 10;
            //    animate.spanTime = 10;
            //    animate.g = this.GetGraphics();
            //    animate.line.from = new Point(600, 200 + i * 20);
            //    animate.line.end = new Point(10, 200 + i * 20);
            //    animate.line.pen = new Pen(Color.Red, 3);
            //    DrawPaths tmp = new DrawPaths();
            //    tmp.animate = new Animate[1] { animate };
            //    tmp.control = this;
            //    CommonTool.StartDrawPathsThread(tmp);
            //}
        }

        private void InstructionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
        }

        private void MainForm_Move(object sender, EventArgs e)
        {
            if (commandForm != null && commandForm.Visible)
            {
                if (commandForm.isTouchedWithParent)
                {
                    commandForm.SetPosition();
                }
                else
                {
                    if(commandForm.JudgeIfToTouch())
                    {
                        commandForm.isTouchedWithParent = true;
                        commandForm.SetPosition();
                    }
                }
            }
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (commandForm!=null)
            {
                commandForm.Close();
            }
            if (image != null)
            {
                image.Dispose();
            }
        }

        private Image LoadImage(string path)
        {
            Image image = Image.FromFile(path);
            Image tmp = new Bitmap(image,image.Width, MainFormMenu.Height + image.Height);
            Graphics g = Graphics.FromImage(tmp);
            g.DrawImage(image, new Point(0, MainFormMenu.Height));
            image.Dispose();
            return tmp;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            image = this.LoadImage("PipeLine.png");
            this.ClientSize = new Size(image.Width,MainFormMenu.Height + image.Height);
            CommonTool.InitialPositions("position.xml");
        }

        public Graphics[] GetGraphics()
        {
            if (gs == null)
            {
                gs = new Graphics[] {this.CreateGraphics(), Graphics.FromImage(image)};
            }
            return gs;
        }
    }
}
